package com.uenpay.im.ui.old.activity.choosemorepic;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qiyunxin.android.http.common.ImageUtil;
import com.qiyunxin.android.http.utils.FileUtils;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.entities.ImageSize;
import com.uenpay.util.app.base.BaseActivity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class TakePhotoActivity extends BaseActivity {

    private final int CHOOSE_TAKE_PHOTO_REQUEST_CODE = 110;
    private String currentPhotoPath;
    private FileUtils fileUtils = new FileUtils(IMApplication.appName);

    private TextView middleText;

    private View top_view;

    private MaskImageView photo_img;

    private String big_pic_filename;

    private ArrayList<ImageSize> sizes;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.take_photo;
    }

    @Override
    public void initView(View view) {
        Bundle bundle = getIntent().getExtras();
        sizes = (ArrayList<ImageSize>) bundle.getSerializable("size");
        initView();
        initListener();
        Intent getImageByCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            File f = fileUtils.createImageFile();
            currentPhotoPath = f.getAbsolutePath();
            getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(getImageByCamera,
                    CHOOSE_TAKE_PHOTO_REQUEST_CODE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void initView() {
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        top_view = findViewById(R.id.top_view);

        top_view.setVisibility(View.GONE);

        // findViewById(R.id.backImageView).setVisibility(View.GONE);

        photo_img = (MaskImageView) findViewById(R.id.photo_img);

        ((TextView) findViewById(R.id.title_right_tv)).setText(R.string.sure);

        middleText = (TextView) findViewById(R.id.title_textview);
        middleText.setText(R.string.yulan);
    }

    protected void initListener() {
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent data = new Intent();
                        data.putExtra("big_pic_filename", big_pic_filename);
                        setResult(RESULT_OK, data);

                        finish();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == CHOOSE_TAKE_PHOTO_REQUEST_CODE) {// ����
                try {
                    big_pic_filename = fileUtils.getAlbumDir() + "/"
                            + Utils.md5(currentPhotoPath) + ".jpg";
                    if (sizes != null) {
                        for (int i = 0; i < sizes.size(); i++) {
                            ImageSize size = sizes.get(i);
                            ImageUtil.CompressImageAndSave(currentPhotoPath,
                                    big_pic_filename + size.name, 80,
                                    size.width, size.height);
                        }
                    }

                    photo_img.SetUrl(big_pic_filename);
                    photo_img.setVisibility(View.VISIBLE);
                    top_view.setVisibility(View.VISIBLE);

                } catch (Exception e) {

                    e.printStackTrace();
                }
            }

        } else {
            finish();
        }

    }
}
